import {defineAsyncComponent} from 'vue'
const files = import.meta.glob('./modules/*.ts');

class GlobalStore {
    key: string;
    value: any;

    constructor(key: string, value: any) {
        this.key = key;
        this.value = value;
    }
}

const globalStore: Array<GlobalStore> = [];
Object.keys(files).forEach(fileName => {
    const key = fileName.replace("./", "")
        .split("/")[fileName.replace("./", "").split("/").length - 1]
        .replace(".ts", "");
    const gs = new GlobalStore(key, files[fileName] as any);
    globalStore.push(gs);
});

// 统一导出所有的store
export default {
    install(App) {
        globalStore.forEach(gs => {
            // 拼接组件名称
            const componentName = "use" + gs.key.charAt(0).toUpperCase() + gs.key.substring(1) + "Store";
            // 通过 defineAsyncComponent 异步加载store组件
            App.component(componentName, defineAsyncComponent(gs.value))
        })
    }
}
